package com.nip.bbdicforandroid.utilities;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.res.AssetManager;

public class AssetsUtilities {

	public static InputStream getInputStream(AssetManager manager,
			String fileName) throws IOException {

		return manager == null ? null : manager.open(fileName);
	}

	/**
	 * Copy tất cả file in
	 * @param manager
	 * @param outFile
	 * @return
	 * @throws IOException
	 */
	public static Boolean copyFilesAssets(AssetManager manager, File outFile)
			throws IOException {
		String[] pathFiles = manager.list("");
		
		if (pathFiles == null) {
			return false;
		}
		byte[] buffer;
		int read;
		for (String pathFile : pathFiles) {
			InputStream in = manager.open(pathFile);
			OutputStream out = new FileOutputStream(new File(outFile, pathFile));
			read = -1;
			buffer = new byte[1024];
			while ((read = in.read(buffer)) != -1) {
				out.write(buffer, 0, read);
			}
			in.close();
			in = null;
			out.flush();
			out.close();
			out = null;
		}

		return true;
	}
}
